package com.UnionPayCenter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.UnionPayCenter.entity.CouponEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.vo.DataBoardHistoryDataVo;
import com.UnionPayCenter.entity.vo.DataBoardTodayDataVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IDataBoardService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: UnionPayCenter
 * @description: 数据看板
 * @author BaoJF
 *
 */
@Slf4j
@Api(value = "dataBoard", tags = "数据看板")
@RestController
@RequestMapping("/dataBoard")
public class DataBoardController {
    @Autowired
    private IDataBoardService dataBoardService;

    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/todayData", method = RequestMethod.POST)
    @ApiOperation(tags = "数据看板", httpMethod = "POST", value = "[后台]查询今日实时概况", response = DataBoardTodayDataVo.class)
    public ResponseWrapper todayData(
            @ApiJsonObject(name = "dataBoardTodayDataParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            DataBoardTodayDataVo todayData = dataBoardService.getTodayData(jsonParam);
            return ResponseWrapper.succeed(todayData);
        }catch (ApiException e){
            log.error(String.format("[后台]查询今日实时概况失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询今日实时概况失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @CurrentShop
    @CurrentManager
    @RequestMapping(value = "/historyData", method = RequestMethod.POST)
    @ApiOperation(tags = "数据看板", httpMethod = "POST", value = "[后台]查询历史概况", response = DataBoardHistoryDataVo.class)
    public ResponseWrapper historyData(
            @ApiJsonObject(name = "dataBoardHistoryDataParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int")
            })@RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            DataBoardHistoryDataVo historyData = dataBoardService.getHistoryData(jsonParam);
            return ResponseWrapper.succeed(historyData);
        }catch (ApiException e){
            log.error(String.format("[后台]查询历史概况失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询历史概况失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
